package cn.hbu.zlfw.admin.dao.impl;

import java.util.Map;
import java.lang.StringBuffer;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import cn.hbu.zlfw.admin.dao.UnitAdminDao;
import cn.hbu.zlfw.admin.entity.UnitAdmin;
import cn.hbu.zlfw.common.base.dao.impl.BaseDaoImpl;
import cn.hbu.zlfw.common.util.PageModel;
import cn.hbu.zlfw.common.util.Utility;

/**
 * 
 * @ClassName: UnitAdminDaoImpl 
 * @Description: 单位管理员dao接口实现
 * @author 王鹏伟<1223443289@qq.com>
 * @date 2017年11月27日 下午10:27:15 
 * @vsersion:v1.0
 */
@Repository("unitAdminDao")
public class UnitAdminDaoImpl extends BaseDaoImpl<UnitAdmin, String> implements UnitAdminDao{
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.UnitAdminDao#getUnitAdminByParam(java.util.Map)
	 */
	@Override
	public UnitAdmin getUnitAdminByParam(Map<String, Object> params) {
		StringBuffer sbf = new StringBuffer();
		sbf.append("from UnitAdmin where ");
		for (Map.Entry<String, Object> param : params.entrySet()) {
			sbf.append(param.getKey()+"=:"+param.getKey()+" and ");
		}
		sbf.append(" delState=:delState ");
		params.put("delState", 0);
		Query query=this.getSession().createQuery(sbf.toString());
		return (UnitAdmin) Utility.queryParameterUtil(query, params).uniqueResult();
	}
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.UnitAdminDao#listUniAdminByParamsToRoot(cn.hbu.zlfw.common.util.PageModel, java.lang.String, java.lang.Integer)
	 */
	@Override
	public PageModel<UnitAdmin> listUniAdminByParamsToRoot(PageModel<UnitAdmin> pageModel, String userName,
			Integer schoolType) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("delState", 0);	//只查询"未删除"科技服务人员
		StringBuffer sbfhql = new StringBuffer();
		sbfhql.append("from UnitAdmin where delState=:delState ");
		if(userName != null && userName != "") {
			params.put("userName", "%" + userName + "%");
			sbfhql.append("and userName like :userName ");
		}
		if(schoolType != null && schoolType != 0) {
			params.put("schoolType", schoolType);
			sbfhql.append("and schoolType = :schoolType ");
		}
		return this.findListByParams(sbfhql.toString(), null, params, pageModel);
	}
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.UnitAdminDao#getUnitAdminToUpdate(java.lang.String, java.lang.String)
	 */
	@Override
	public UnitAdmin getUnitAdminToUpdate(String phoneNumber, String id) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("id", id);
		params.put("phoneNumber", phoneNumber);
		params.put("delState", 0);	//只查询"未删除"科技服务人员
		String hql = "from UnitAdmin where id != :id and phoneNumber=:phoneNumber and delState=:delState";
		Query query = this.getSession().createQuery(hql);
		return (UnitAdmin) Utility.queryParameterUtil(query, params).uniqueResult();
	}
	/*
	 * (non-Javadoc)
	 * @see cn.hbu.zlfw.admin.dao.UnitAdminDao#getUnitAdminByParams(java.lang.String)
	 */
	@Override
	public UnitAdmin getUnitAdminByParams(String phoneNumber) {
		Map<String, Object> params =Utility.getMapInstance();
		params.put("phoneNumber", phoneNumber);
		params.put("delState", 0);	//只查询"未删除"科技服务人员
		String hql = "from UnitAdmin where phoneNumber=:phoneNumber and delState=:delState";
		Query query = this.getSession().createQuery(hql);
		return (UnitAdmin) Utility.queryParameterUtil(query, params).uniqueResult();
	}

}
